package org.lztvn.elearning.front_controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.servlet.http.HttpSession;

import org.hibernate.annotations.Parameter;
import org.lztvn.elearning.controllers.AppController;
import org.lztvn.elearning.entities.Question;
import org.lztvn.elearning.entities.Unit;
import org.lztvn.elearning.entities.User;
import org.lztvn.elearning.service.IQuestionService;
import org.lztvn.elearning.service.IResponseService;
import org.lztvn.elearning.service.IUnitService;
import org.lztvn.elearning.service.JsonResponse;
import org.lztvn.elearning.service.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@SessionAttributes("userFLogin")
public class UnitController extends AppController {
	@Autowired
	IUnitService serviceUnit;
	@Autowired
	IResponseService serviceRes;
	@Autowired
	IQuestionService serviceQuest;
	
	JsonResponse jresp;

	private void init() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		this.jresp = (JsonResponse) context.getBean("json");
	}

	@RequestMapping(value = "/list_unit", method = RequestMethod.GET, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse getListUnitByCourseId() throws Exception {
		init();
		List<Unit> list_unit = serviceUnit.listUnitByCourseId();
		jresp.setResult(list_unit);

		return jresp;
	}
	
	
	@RequestMapping(value = "/list_quest/{idUnit}", method = RequestMethod.GET, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse getListQuestionByUnitId(@PathVariable(value="idUnit") Long idUnit) throws Exception {
		init();
		List<Question> list_quest =  serviceQuest.getlistQuestionbyTestId(idUnit);
		System.out.println("-======================-- " +list_quest.size());
		jresp.setResult(list_quest);

		return jresp;
	}
	
	@RequestMapping(value = "/unit_detail/{idUnit}", method = RequestMethod.GET, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse getByUnitId(@PathVariable(value="idUnit") Long idUnit) throws Exception {
		init();
		Unit u = serviceUnit.getUnit(idUnit);
		jresp.setResult(u);

		return jresp;
	}

	@RequestMapping(value = "/quest_detail/{idQuest}", method = RequestMethod.GET, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse getByQuestId(@PathVariable(value="idQuest") Long idQuest) throws Exception {
		init();
		Question q = serviceQuest.getQuestion(idQuest);
		jresp.setResult(q);

		return jresp;
	}
	
	@RequestMapping(value = "/update_unit/{idUnit}/{pos}", method = RequestMethod.GET, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse update_unit(@PathVariable(value="idUnit") Long idUnit, @PathVariable(value="pos") Integer pos) throws Exception {
        init();
		Unit u = serviceUnit.getUnit(idUnit);
		u.setPos(pos);
		serviceUnit.modifierUnit(u);
		jresp.setStatus("SUCCESS");

		return jresp;
	}

	
	@RequestMapping(value = "/delete_unit/{idUnit}", method = RequestMethod.GET, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse delete_unit(@PathVariable(value="idUnit") Long idUnit) throws Exception {
        init();
		serviceUnit.deleteUnit(idUnit);
		serviceQuest.delQuestionbyUnitId(idUnit);
		jresp.setStatus("SUCCESS");
		return jresp;
	}

	
	@RequestMapping(value = "/delete_quest/{idq}", method = RequestMethod.GET, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse delete_quest(@PathVariable(value="idq") Long idq) throws Exception {
        init();
		serviceQuest.delQuest(idq);
		jresp.setStatus("SUCCESS");
		return jresp;
	}
	
	@RequestMapping(value = "/save_unit", method = RequestMethod.POST, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse save_unit(@RequestBody Unit u) throws Exception {
        init();
        if (u.getIdUnit() != null) {
        try {
        	serviceUnit.modifierUnit(u);
        } catch (Exception e) {
		   System.out.println(e);
		}
		jresp.setStatus("Unit is modified");
        }
        else{
        	serviceUnit.addUnit(u);
        	jresp.setStatus("Unit is saved");
        }
		return jresp;
	}

	
	@RequestMapping(value = "/save_question", method = RequestMethod.POST, produces = {
			"application/xml", "application/json" })
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody JsonResponse save_question(@RequestBody Question q,  HttpSession session) throws Exception {
        init();
        if ( q.getQestId()!= null) {
        try {
        	serviceQuest.editQuest(q);
        } catch (Exception e) {
		   System.out.println(e);
		}
		jresp.setStatus("Question is modified");
        }
        else{
            User u = (User) session.getAttribute("userFLogin");
        	serviceQuest.addQuest(q);
        	jresp.setStatus("Question is saved");
        }
		return jresp;
	}
}
